package controllers.commands
{
	import com.google.maps.LatLng;
	import com.google.maps.LatLngBounds;
	import com.google.maps.Map;
	import com.google.maps.MapType;
	import com.google.maps.overlays.GroundOverlay;
	import com.google.maps.overlays.GroundOverlayOptions;
	import com.google.maps.overlays.Marker;
	import com.google.maps.overlays.MarkerOptions;
	import com.google.maps.overlays.Polygon;
	import com.google.maps.overlays.PolygonOptions;
	
	import controllers.commands.base.ICommand;
	import controllers.managers.SingletonManager;
	import controllers.managers.StateManager;
	
	import model.assets.AssetsModel;
	import model.context.StartEleBoundaryProtocalContext;
	import model.states.MaperState;
	
	import mx.containers.Canvas;
	
	import view.panel.LocationInfoPanel;

	/**
	 * 启动电子围栏
	 **/
	public class StartElectricBudryCommand implements ICommand
	{
		private var _map:Map;
		private var _polyG:Polygon=null;
		private var _startLatlng:LatLng=null;
		private var _endLatlng:LatLng=null;
		private var _isPress:Boolean=false;

		public function StartElectricBudryCommand(map:Map)
		{
			this._map=map;				
		}

		public function setContext(context:Object):void
		{

		}

		/**
		 * 鼠标按下事件，生成图元
		 **/
		public function onMDownHandler(latlng:LatLng):void
		{
			_startLatlng=latlng;
			_endLatlng=latlng;
			

			_isPress=true;

			updateLocation(latlng);
//			
//			if (this._isPress)
//			{
//				this._map.addOverlay(_polyG);
//			}
		}

		/**
		 * 鼠标释放事件，删除当前图元
		 **/
		public function onMUpHandler(latlng:LatLng):void
		{
			trace("Sending Protocal..... ");
			trace("start " + _startLatlng  + " _endLatlng:" + _endLatlng );
			
			var  gContext : StartEleBoundaryProtocalContext 
							= StartEleBoundaryProtocalContext(SingletonManager.getServ(StartEleBoundaryProtocalContext.NAME));
			
		 	 gContext.startLatlnt = _startLatlng;
			 gContext.endLatlnt = _endLatlng;
			 
			_isPress=false;
		}

		/**
		 * 在地图上鼠标移动事件
		 **/
		public function onMMovingHandler(latlng:LatLng):void
		{
			if (_startLatlng != null && _endLatlng != null && this._isPress)
			{
					updateLocation(latlng);
					execute();
			}
		}

		private function updateLocation(latlng:LatLng):void
		{
			_endLatlng=latlng;
			_polyG=new Polygon([_startLatlng, 
				new LatLng(_endLatlng.lat(), _startLatlng.lng()), 
				_endLatlng, 
				new LatLng(_startLatlng.lat(), _endLatlng.lng())], 
				new PolygonOptions());
		}
		
		private function updateRect():void
		{
			clear();		
			this._map.addOverlay(_polyG);
		}
		
		private function clear():void
		{
			if (_polyG != null)
			{
				this._map.clearOverlays();
			}
		}

		public function execute():void
		{
			updateRect();
		}	
	}
}
